package com.cclinux.projects.actcampus.comm;

import com.aliyun.oss.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * OSS工具类
 *
 * @author lyn
 **/
@Slf4j
public class OSSUtil {
//    private String endpoint = "http://oss-cn-beijing.aliyuncs.com";
//    private String accessKeyId = "LTAI4Feu6Gt7eg6oXnqoB1nb";
//    private String accessKeySecret = "pe5ZB6JYdrJDwBmhYzFWvxcor6E5Ex";
//    private String bucketName = "xiguandakaxingdong";

//
//    access-key-secret: PisUUBJxvITWwydeYiN2nM7ZY5LCm7
//    endpoint: oss-cn-beijing-internal.aliyuncs.com
//    bucket-name: harves-it
//    image-url-prefix: image.harves-e.com
//    private-bucket-name: harves-it-private


//    private String endpoint = "http://oss-cn-beijing.aliyuncs.com";
//    private String accessKeyId = "LTAI4FxbBPyyPuqx1TMGGL4E";
//    private String accessKeySecret = "OxU7jYZKcLyAArkPZyeKjdMwtOcUjM";
//    private String bucketName = "chucun2019";
//    public static final String DYNAMIC_FILEDIR = "dynamic/";
//    public static final String USER_FILEDIR = "user/";
//    public static final String TEAM_FILEDIR = "team/";
//
//    public String visit = "https://chucun2019.oss-cn-beijing.aliyuncs.com";




    private String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    private String accessKeyId = "LTAI5tLAmFx5GmatFM14Z7a1";
    private String accessKeySecret = "PisUUBJxvITWwydeYiN2nM7ZY5LCm7";
    private String bucketName = "harves-it";
    public static final String DYNAMIC_FILEDIR = "football/";

    public String visit = "https://harves-it.oss-cn-beijing.aliyuncs.com";

    private OSSClient createOss() {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }

    public String putDynamicObject(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            log.info("文件不能为空");
            return null;
        }
        String objectName = subImgName(file.getOriginalFilename());
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            return putObject(DYNAMIC_FILEDIR + dateFormat.format(new Date()) + "/",objectName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public String putObject(String filedirName,String objectName, InputStream inputStream) {
        OSSClient ossClient = null;
        try {
            ossClient = createOss();
            ossClient.putObject(bucketName, filedirName + objectName, inputStream);
            String url = getUrl(filedirName,objectName);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "";
    }

    public String subImgName(String originalFilename) {
        if (TextUtils.isEmpty(originalFilename)) {
            return null;
        }
        String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        Random random = new Random();
        String name = (long) random.nextInt(10000) + System.currentTimeMillis() + substring;
        return name;
    }

    public String getUrl(String filedir,String objectName) {
        String url = visit + "/" + filedir + objectName;
        return url;
    }

    public String getFullUrl(String objectName, OSSClient ossClient) {
        Date expiration = new Date(System.currentTimeMillis() + 315360000000L);
        try {
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
            if (url != null) {
                String var5 = url.toString();
                return var5;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
